package com.risetechnology.utils.threads;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.risetechnology.common.Shared;
import com.risetechnology.data.RecipeUpdate;

public class PurchaseRecipeTask extends AsyncTask<Void, Void, String> {
	ProgressDialog dialog;
	String email;
	Context context;
	RecipeUpdate recipeUpdate;

	public PurchaseRecipeTask(Context context, String email,
			RecipeUpdate recipeUpdate) {
		dialog = new ProgressDialog(context);
		dialog.setMessage("Loading...");
		dialog.setCancelable(false);
		this.context = context;
		this.email = email;
		this.recipeUpdate = recipeUpdate;
	}

	@Override
	protected String doInBackground(Void... params) {
		try {
			HttpParams httpParameters = new BasicHttpParams();
			// Set the timeout in milliseconds until a connection is
			// established.
			int timeoutConnection = 30000;
			HttpConnectionParams.setConnectionTimeout(httpParameters,
					timeoutConnection);
			// Set the default socket timeout (SO_TIMEOUT)
			// in milliseconds which is the timeout for waiting for
			// data.
			int timeoutSocket = 30000;
			HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
			HttpClient httpclient = new DefaultHttpClient(httpParameters);
			HttpPost httppost = new HttpPost(
					"http://cloud2.rise.ws/krystalkitchen/webservices/buyPackage.php");
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("email", email));
			nameValuePairs.add(new BasicNameValuePair("package_id", recipeUpdate
					.getId()));

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			httpclient.execute(httppost);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return e.getMessage();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return e.getMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	protected void onPreExecute() {
		dialog.show();
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(String result) {
		dialog.dismiss();
		if (result == null) {
			
			if(recipeUpdate.isDownloaded()){
				Shared.dbAdapter.unBlockRecipe(recipeUpdate.getId());
			}else{
				new DownloadRecipeTask(context, recipeUpdate, false).execute();
			}
		}
		super.onPostExecute(result);
	}

}
